package com.serkol.abmanager.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class SwaggerConfig {
    Boolean swaggerEnabled=true;
    @Bean
    public Docket createRestApi() {

        Docket docket = new Docket(DocumentationType.OAS_30);
docket.enable(swaggerEnabled);
        ApiSelectorBuilder select = docket.apiInfo(apiInfo()).select();

        select.apis(RequestHandlerSelectors.basePackage("com.serkol.abmanager.controller.api"))
                .paths(PathSelectors.any()).build().globalRequestParameters(getGlobalRequestParameter()).globalResponses(HttpMethod.GET, getGlobalResponseMessage()).globalResponses(HttpMethod.POST, getGlobalResponseMessage());

        return docket;
  /*      return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo())
                // 是否开启
                .enable(swaggerEnabled).select()
                // 扫描的路径包
                .apis(RequestHandlerSelectors.basePackage("com.serkol.abmanager.controller.api"))
                // 指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any()).build().pathMapping("/");*/
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot-Swagger3集成和使用-demo示例")
                .description("springboot | swagger")
                // 作者信息
                .contact(new Contact("Hesenjan", "http://www.baidu.com", "1533350610@qq.com"))
                .version("1.0.0")
                .build();
    }
    private List<RequestParameter> getGlobalRequestParameter() {
        List<RequestParameter> requestParameters = new ArrayList<>();
        RequestParameterBuilder builder = new RequestParameterBuilder();
        builder.name("Authorization");
        builder.description("平台id");
        //  builder.required(true);
        builder.in(ParameterType.HEADER);

        builder.query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)));
        builder.required(false);
        //   builder.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header")
        //    .required(false).build();
        requestParameters.add(builder.build());



        builder.name("terminal");
        builder.description("平台id");
        builder.required(true);
        builder.in(ParameterType.HEADER);

        builder.query(q -> q.model(m -> m.scalarModel(ScalarType.INTEGER)));
        builder.required(false);
        requestParameters.add(builder.build());




        return requestParameters;
    }

    private List<Response> getGlobalResponseMessage() {
        List<Response> responseList = new ArrayList<>();
        springfox.documentation.service.Response build = new ResponseBuilder().code("404").description("  meyou page").build();
        responseList.add(build);
        return responseList;
    }
}